package com.xyw.code.efficient_programming.lambda.stream;

import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Created by xuyiwei on 2020/1/6 18:47
 * ；流的四种构建方式
 */
public class StreamConstructor {

    /**
     * 通过构造方法
     */
    public void streaFromValue(){
        Stream stream = Stream.of(1,2,3,4,5);
        stream.forEach(System.out::println);
    }

    /**
     * 通过数组
     */
    public void streamFromArray(){
        int [] nums = {1,2,3,4,5};
        IntStream stream = Arrays.stream(nums);
        stream.forEach(System.out::println);
    }

    /**
     * 通过file生产流
     */
    @Test
    public void streamFromFile() throws IOException {
        Stream<String> stream = Files.lines(Paths.get("E:\\xywWorkSpace\\efficient_programming\\src\\main\\java\\com\\xyw\\code\\efficient_programming\\lambda\\stream\\StreamVs.java"));
        stream.forEach(System.out::println);
    }

    /**
     * 通过函数生产流
     */
    @Test
    public void streamFromFunction(){
        Stream stream = Stream.iterate(0,n->n+2);
        Stream stream1 = Stream.generate(Math::random);
        stream1.limit(100).forEach(System.out::println);
    }
}
